package com.han.shadow.customerapp.adapter;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.han.shadow.customerapp.R;
import com.han.shadow.customerapp.bean.Business;
import com.han.shadow.customerapp.bean.Goods;

import java.util.List;
import java.util.Map;

/**
 * Created by ACE on 2016/4/23 0023.
 */
public class MyCartAdapter extends BaseAdapter {

    private final Handler pHandler;
    private List<Goods> goodses = null;
    private LayoutInflater inflater;
    private Map<String,Integer> cart;
    private String[] names = null;
    private TextView textCartCount;

    public MyCartAdapter(List<Goods> goodses, Map<String,Integer> cart, LayoutInflater inflater, Handler handler){
        this.goodses = goodses;
        this.inflater = inflater;
        this.pHandler = handler;
        this.cart = cart;

        if(cart != null && cart.size() >0) {
            int i = 0;
            names = new String[cart.size()];
            for (Map.Entry<String, Integer> entry : cart.entrySet()) {
                names[i++] = entry.getKey();
            }
        }

    }

    @Override
    public int getCount() {
        return cart.size();
    }

    @Override
    public Object getItem(int position) {
        return cart.get(names[position]);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        convertView = inflater.inflate(R.layout.activity_cart_style,null);
        final Goods goods = findGoods(position);

        if(goods != null) {
            TextView textCartName = (TextView) convertView.findViewById(R.id.textCartName);
            textCartName.setText(goods.getName());

            TextView textCartPrice = (TextView) convertView.findViewById(R.id.textCartPrice);
            textCartPrice.setText((float) (goods.getPrice() / 10) + "元/份");

            textCartCount = (TextView) convertView.findViewById(R.id.textCartCount);
            textCartCount.setText(String.valueOf(cart.get(goods.getGid())));

            Button btnCartSub = (Button) convertView.findViewById(R.id.btnCartSub);
            btnCartSub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message msg = new Message();
                    msg.what = 4;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", goods.getGid());
                    msg.setData(bundle);
                    pHandler.sendMessage(msg);

//                    int count = cart.get(goods.getId());
//                    count -= 1;
//                    if(count <= 0) count = 0;
//                    cart.put(goods.getId(),count);
                    Message msg2 =new Message();
                    msg2.what = 1;
                    //msg2.arg1 = count;
                    handler.sendMessage(msg2);

                }
            });

            Button btnCartPlus = (Button) convertView.findViewById(R.id.btnCartPlus);
            btnCartPlus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message msg = new Message();
                    msg.what = 3;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", goods.getGid());
                    msg.setData(bundle);
                    pHandler.sendMessage(msg);


//                    int count = cart.get(goods.getId());
//                    count += 1;
//                    cart.put(goods.getId(),count);
                    Message msg2 =new Message();
                    msg2.what = 1;
                    //msg2.arg1 = count;
                    handler.sendMessage(msg2);

                }
            });
        }

        return convertView;
    }

    private Goods findGoods(int position){
        if(names != null && names.length > 0) {
            for (int i = 0; i < goodses.size(); i++) {
                if (goodses.get(i).getGid().equals(names[position])) {
                    return goodses.get(i);
                }
            }
        }
        return null;
    }


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
//            if(msg.what == 1){
//                String s = String.valueOf(msg.arg1);
//                textCartCount.setText(s);
//            }
            notifyDataSetChanged();
        }
    };
}
